package com.zxmr.hospital.customer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.RequestPayload;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Customer;
import com.zxmr.domain.Report;
import com.zxmr.domain.service.CustomerSearchBean;
import com.zxmr.domain.service.CustomerService;
import com.zxmr.hospital.customer.dto.CustomerAnsysisDTO;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.Utils;

public class CustomerAnsysis extends Executor{

	@Override
	public void execute(Request request, Response response) {
		
		try
		{
			exe(request,response);
		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue(ResourceManager.getInstance().getMessage("com.zxmr.customer.internalError"));
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
		//ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		//response.setPayload(payload);
	}
	private void exe(Request request,Response response)throws ExecuteException
	{
		CustomerSearchBean searchBean = parseSearchBean(request);
		
		CustomerService service = new CustomerService();
		List<Customer> customers = service.queryCustomerForAnsysis(searchBean);
		
		CustomerAnsysisDTO dto = ansysis(customers);
		forwardToUtility(request,dto);
	}
	
	private void forwardToUtility(Request request,CustomerAnsysisDTO dto)throws ExecuteException
	{
		String reportTitle = request.getRequestParameters().get("reportTitle");
		String reportDesc = request.getRequestParameters().get("reportDesc");
		
		if(reportTitle == null)
			reportTitle = "customer analysis "+Utils.getCurrentTime();
		
		// construct request payload
		RequestPayload payload = new RequestPayload();
		payload.setUrl("/report/add");
		Report report = new Report();
		report.setContent(JSON.toJSONString(dto));
		report.setTheme("component/customer/customerAnalysis.html");
		report.setTitle(reportTitle);
		report.setDescription(reportDesc);
		report.setCompany(request.getAccount().getCompany());
		report.setOwner(request.getAccount().getName());

		
		Map<String,String> para = new HashMap<String,String>();
		para.put("parameter", JSON.toJSONString(report));
		payload.setParameters(para);
		
		request.setPayload(payload);
		this.redirect(request, "zxmr.utility");
	}
	private CustomerSearchBean parseSearchBean(Request request)throws ExecuteException
	{
		String value = request.getRequestParameters().get("value");
		String location = request.getRequestParameters().get("location");
		String items = request.getRequestParameters().get("items");
		String type = request.getRequestParameters().get("type");

		
		CustomerSearchBean searchBean = new CustomerSearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setLocation(location);
		searchBean.setValue(value);
		searchBean.setItems(items);
		searchBean.setType(type);
		
		return searchBean;
	}
	private CustomerAnsysisDTO ansysis(List<Customer> customers)
	{
		if(customers == null)return null;
		
		CustomerAnsysisDTO dto = new CustomerAnsysisDTO();
		
		Map<String,Integer> provinces = new HashMap<String,Integer>();
		int provinceTotal = 0;
		Map<String,Integer> cities = new HashMap<String,Integer>();
		int cityTotal = 0;
		Map<String,Integer> ages = new HashMap<String,Integer>();
		int ageTotal = 0;
		Map<String,Integer> works = new HashMap<String,Integer>();
		int workTotal = 0;
		Map<String,Integer> items = new HashMap<String,Integer>();
		int itemTotal = 0;
		
		for(Customer customer : customers)
		{

			if(!Utils.isStringEmptyWithTrim(customer.getProvince()))
			{
				increaseCount(provinces,customer.getProvince());
				provinceTotal++;
			}
			if(!Utils.isStringEmptyWithTrim(customer.getCity()))
			{
				increaseCount(cities,customer.getCity());
				cityTotal++;
			}
			if(!Utils.isStringEmptyWithTrim(customer.getWork()))
			{
				increaseCount(works,customer.getWork());
				workTotal++;
			}
			
			if(customer.getAge() > 0)
			{
				increaseCount(ages,String.valueOf(customer.getAge()));
				ageTotal++;
			}
			if(!Utils.isStringEmptyWithTrim(customer.getItems()))
			{
				String[] its = customer.getItems().split(",");
				for(String item : its)
				{
					increaseCount(items,item);
					itemTotal++;
				}
			}
		}
		for(String key : provinces.keySet())
		{
			int count = provinces.get(key);
			Double d = count * 100.0;
			List<Object> attr = new ArrayList<Object>();
			attr.add(key);
			attr.add(d/provinceTotal);
			dto.getProvinces().add(attr);
		}
		for(String key : cities.keySet())
		{
			int count = cities.get(key);
			Double d = count * 100.0;
			
			List<Object> attr = new ArrayList<Object>();
			attr.add(key);
			attr.add(d/cityTotal);
			
			dto.getCities().add(attr);
		}
		for(String key : ages.keySet())
		{
			int count = ages.get(key);
			Double d = count * 100.0;
			
			List<Object> attr = new ArrayList<Object>();
			attr.add(key);
			attr.add(d/ageTotal);
			
			dto.getAges().add(attr);
		}
		for(String key : works.keySet())
		{
			int count = works.get(key);
			Double d = count * 100.0;
			
			List<Object> attr = new ArrayList<Object>();
			attr.add(key);
			attr.add(d/workTotal);
			
			dto.getWorks().add(attr);
		}
		for(String key : items.keySet())
		{
			int count = items.get(key);
			Double d = count * 100.0;
			
			List<Object> attr = new ArrayList<Object>();
			attr.add(key);
			attr.add(d/itemTotal);
			
			dto.getItems().add(attr);
		}
		
		return dto;
	}
	private void increaseCount(Map<String,Integer> maps, String value)
	{
		if(maps.containsKey(value))
		{
			int v = maps.get(value);
			maps.put(value, v+1);
		}else
		{
			maps.put(value, 1);
		}
	}

}
